//============================================================================
// Name        : MediaRecorder.cpp
// Author      : chuanjiang.zh
// Version     :
// Copyright   : Copyright 2017 100wits.com
// Description : Hello World in C, Ansi-style
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include "MediaWriter.h"
#include "RawMediaWriter.h"
#include "CMediaWriter.h"
#include "Ffmpeg.h"
#include "CLog.h"
#include "PsMediaWriter.h"

namespace av
{


void MediaWriterFactory::startup()
{
	av_register_all();
	avformat_network_init();

	if (CLog::startup("MediaWriterLog.ini") != 0)
	{
#ifdef WIN32
		CLog::setLogger(CLog::COUT);
		CLog::setLogger(CLog::DEBUGWINDOW);
#endif //
	}
}

void MediaWriterFactory::cleanup()
{
	avformat_network_deinit();
}

const char* MediaWriterFactory::getFileExt(MediaFileFormat fileFormat)
{
	if (fileFormat == FILE_RAW)
	{
		return ".raw";
	}
	else if (fileFormat == FILE_TS)
	{
		return ".ts";
	}
	else if (fileFormat == FILE_MP4)
	{
		return ".mp4";
	}
	else if (fileFormat == FILE_PS)
	{
		return ".ps";
	}
	return ".av";
}

/**
 * 创建媒体写入对象
 * @param fileFormat	文件格式
 * @return
 */
MediaWriter* MediaWriterFactory::create(MediaFileFormat fileFormat)
{
	if (fileFormat == FILE_RAW)
	{
		return new RawMediaWriter();
	}
	else if (fileFormat == FILE_PS)
	{
		return new PsMediaWriter();
	}

	return new CMediaWriter();
}


/**
 * 销毁媒体写入对象
 * @param writer
 */
void MediaWriterFactory::destroy(MediaWriter* writer)
{
	if (writer)
	{
		writer->close();
		delete writer;
	}

}




}
